package mypackage3;

import java.util.Random;
import java.util.Scanner;

public class RandomCode {
    //生成长度为5,4个字符,1个数字
    static char[] nums=new char[52];
    public static void main(String[] args) {
        init();
        StringBuilder code=creatRandomCode();
        System.out.print(code);
        check(code);
    }
    public static void init(){
        for (int i = 0; i < 52; i++) {
            nums[i]=(char)('A'+i);
            if(i>25){
                nums[i]=(char)('a'+i-26);
            }
        }
    }
    public static StringBuilder creatRandomCode(){
        Random r=new Random();
        char[] ans=new char[5];
        ans[r.nextInt(5)]=(char)('0'+r.nextInt(10));
        for(int i=0;i<5;++i){
            char c=nums[r.nextInt(52)];
            if(ans[i]<'0'){
                ans[i]=c;
            }
        }
        StringBuilder code=new StringBuilder();
        for (int i = 0; i < ans.length; i++) {
            code.append(ans[i]);
            if(i!=ans.length-1){
                code.append(" ");
            }
        }
        return code;
    }
    public static void check(StringBuilder code){
        Scanner sc=new Scanner(System.in);
        while(true){
            System.out.println("\n请输入验证码");
            String str=sc.nextLine();
            if(code.toString().equals(str)){
                System.out.print("验证码输入正确");
                break;
            }else{
                System.out.print("验证码输入错误,请重新输入");
            }
        }
    }
}
